#include <stdio.h>
#include <string.h>
#include "fifo.h"
#include "printf.h"

fifo_t f;


void display(unsigned char *buf, unsigned int len)
{
	unsigned int i ;
	
	for(i = 0; i < len; i++)
	{
		user_data("%02x ", buf[i]);
	}
	
	user_data("\n ");
}



/*测试例程：
	1.设置FIFO总长度为10；
	2.查看当前FIFO状态，数据长度，总长度等；
	3.往FIFO中放入5个数据，并查看FIFO中数据
*/
int main(int argc, char** argv) 
{
	unsigned int ret;
	unsigned char list[10];
	unsigned char writebuf[10] = {0,1,2,3,4,5,6,7,8,9};
	unsigned int addr;
	unsigned char *pbuf;
	unsigned char readbuf[10];

	/*fifo初始化*/
	if(fifo_init(&f, list, 10) == TURE)
	{
		#ifdef 	__PRINTF_H__
		user_debug("init seccess!");
		#endif
	}
		
	/*测试程序*/
	
	/*当前状态*/
	//当前FIFO中数据长度
	#ifdef 	__PRINTF_H__
	user_debug("fifo len = %d ", fifo_len(&f));
	user_debug("fifo size = %d ", fifo_size(&f));
	//判断FIFO是空还是满
	if(fifo_is_empty(&f))
		user_debug("fifo is empty! \n", ret);
	if(fifo_is_full(&f))
		user_debug("fifo is full! \n", ret);
	#endif

	//在FIFO中放入5个数据 FIFO中有五个数据
	ret = fifo_put(&f,writebuf, 5);
		#ifdef 	__PRINTF_H__
		user_debug("fifo put %d! ", ret);
		user_debug("fifo len %d! \n", fifo_len(&f));
		#endif
	
	//在FIFO中放入5个数据 FIFO中有十个数据 满
	ret = fifo_put(&f,writebuf, 5);
		#ifdef 	__PRINTF_H__
		user_debug("fifo put %d! ", ret);
		user_debug("fifo len %d! \n", fifo_len(&f));
		#endif
	
	//在FIFO中放入5个数据 已满 无法再次存放
	ret = fifo_put(&f,writebuf, 5);
		#ifdef 	__PRINTF_H__
		user_debug("fifo put %d! ", ret);
		user_debug("fifo len %d! \n", fifo_len(&f));
		#endif

	//从FIFO中取出5个数据
	ret = fifo_get(&f,(unsigned char*)pbuf, 5);
	
	user_debug("fifo_get = %d ", ret);	
		#ifdef 	__PRINTF_H__
		//输出获取的数据
		user_data("Get data is : ");	
		display(pbuf, 5);
		
		//当前FIFO中缓存总长度
		user_debug("fifo total size = %d ", fifo_size(&f));
		//从FIFO中取出的数量
		user_debug("Remove %d data from fifo! ", ret);
		//当前FIFO中数据长度
		user_debug("fifo data len %d! ", fifo_len(&f));	
		//判断FIFO是空还是满
		if(fifo_is_empty(&f))
			user_debug("fifo is empty! \n", ret);
		else if(fifo_is_full(&f))
			user_debug("fifo is full! \n", ret);
		else
			user_debug("");
		#endif
		
	//从FIFO中取出5个数据
	ret = fifo_get(&f,(unsigned char*)pbuf, 5);
		#ifdef 	__PRINTF_H__
		//输出获取的数据
		user_data("Get data is : ");
		display(pbuf, 5);
		
		//当前FIFO中缓存总长度
		user_debug("fifo total size = %d \n", fifo_size(&f));
		//从FIFO中取出的数量
		user_debug("Remove %d data from fifo! \n", ret);
		//当前FIFO中数据长度
		user_debug("fifo data len %d! \n", fifo_len(&f));	
		//判断FIFO是空还是满
		if(fifo_is_empty(&f))
			user_debug("fifo is empty! \n", ret);
		if(fifo_is_full(&f))
			user_debug("fifo is full! \n", ret);
		#endif
	
	return 0;
}

